﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WHC.Pager.Entity;

using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmCustomerSearch : BaseForm
    {
        public string SelectedID = "";

        public FrmCustomerSearch()
        {
            InitializeComponent();
        }
        public override void RefreshForm()
        {
            BindData();
        }

        private void FrmCustomer_Load(object sender, EventArgs e)
        {
            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;

            this.txtName.Focus();
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = sender as DataGridView;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    FrmEditCustomer dlg = new FrmEditCustomer();
                    dlg.ID = row.Cells["ID"].Value.ToString();
                    if (DialogResult.OK == dlg.ShowDialog())
                    {
                        BindData();
                    }

                    break;
                }
            }
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            PagerInfo pagerInfo = new PagerInfo();
            pagerInfo.CurrenetPageIndex = 1;
            pagerInfo.PageSize = int.MaxValue;
            this.winGridViewPager1.AllToExport = BLLFactory<Customer>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("CustomerType", "宾客类型");
            this.winGridViewPager1.AddColumnAlias("Name", "宾客姓名");
            this.winGridViewPager1.AddColumnAlias("IDCarType", "证件类型");
            this.winGridViewPager1.AddColumnAlias("IDNumber", "证件号码");
            this.winGridViewPager1.AddColumnAlias("Telephone", "联系电话");
            this.winGridViewPager1.AddColumnAlias("Source", "宾客来源");
            this.winGridViewPager1.AddColumnAlias("Sex", "宾客性别");
            this.winGridViewPager1.AddColumnAlias("CompanyName", "公司名称");
            this.winGridViewPager1.AddColumnAlias("Address", "宾客地址");
            this.winGridViewPager1.AddColumnAlias("RegistrationDate", "登记日期");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");

            this.winGridViewPager1.DisplayColumns = "ID,CustomerType,Name,IDCarType,IDNumber,Telephone,Address";
            #endregion

            SearchCondition condition = new SearchCondition();
            condition.AddCondition("IDNumber", this.txtName.Text, SqlOperator.Like, true, "Search")
                .AddCondition("Telephone", this.txtName.Text, SqlOperator.Like, true, "Search")
                .AddCondition("Name", this.txtName.Text, SqlOperator.Like, true, "Search");

            string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
            List<CustomerInfo> list = BLLFactory<Customer>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
            this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<CustomerInfo>(list);

            this.winGridViewPager1.dataGridView1.Refresh();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void SearchControl_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(null, null);
            }
        }

        private void btnSelectClose_Click(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    this.SelectedID = row.Cells["ID"].Value.ToString();

                    break;
                }
            }
            this.DialogResult = DialogResult.OK;
        }
    }
}
